package com.xidian.zhiancloud.controller.systemset;

import com.github.pagehelper.Page;
import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.syset.BpmlList;
import com.xidian.zhiancloud.qo.BaseReqQuery;
import com.xidian.zhiancloud.service.user.BpmcService;
import com.xidian.zhiancloud.service.user.WorkFlowStepService;
import com.xidian.zhiancloud.service.user.WorkFlowUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 流程管理
 */
@RestController
@RequestMapping("/bpmc")
public class BpmcController extends BaseController {

    @Autowired
    BpmcService bpmcService;

    @Autowired
    WorkFlowStepService workFlowStepService;

    @Autowired
    WorkFlowUserService workFlowUserService;

    /**
     * 流程类别列表
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getBpmcList")
    public ResultDTO getBpmcList(){
        Long companyId=this.getSessionUserCompanyId();
        return ResultDTO.successOf(bpmcService.getBpmcList(companyId),
                GeneralResponseEnums.SUCCESS);
    }

    /**
     * 流程列表
     * @param reqdata
     * @param bpmid 流程类别ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getBpmlList")
    public ResultDTO getBpmlList(BaseReqQuery reqdata,
                                 @RequestParam("bpmid") Integer bpmid){
        Page<BpmlList>  pagedata = bpmcService.getBpmlList
                (this.getSessionUserCompanyId(),bpmid,reqdata);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("bpmlList", pagedata);
        data.put("PageNum", pagedata.getPageNum());
        data.put("PageSize", pagedata.getPageSize());
        data.put("PageTotal", pagedata.getPages());
        data.put("total", pagedata.getTotal());
        return ResultDTO.successOf(data,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加流程
     * @param id
     * @param bpmid 流程类别ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/addBpml")
    public ResultDTO addBpml
        (@RequestParam("bpmid") Integer bpmid,BpmlList bpml,
         @RequestParam(value = "id",required = false,
             defaultValue = "0") Long id){
        bpml.setBpmid(bpmid);Date d = new Date();
        SimpleDateFormat sbf = new SimpleDateFormat
                ("yyyy-MM-dd HH:mm:ss");
        //去除字符串时间的横杠、空格、冒号
        String bpmnum= sbf.format(d).
                replaceAll("[[\\s-:punct:]]","");
        if(bpmid == 1){//风险辨识评价类
            bpml.setBpmnum("fxbspj" + bpmnum);
        }else if(bpmid == 2){//隐患排查治理类
            bpml.setBpmnum("yhpczl" + bpmnum);
        }else if(bpmid == 3){//危险作业审批类
            bpml.setBpmnum("wxzysp" + bpmnum);
        }else if(bpmid == 4){//危险作业延期审批类
            bpml.setBpmnum("wxzyyqsp" + bpmnum);
        }else if(bpmid == 5){//事故审批类
            bpml.setBpmnum("sgspl" + bpmnum);
        }else if(bpmid == 6){//危化品领用申请类
            bpml.setBpmnum("whplysq" + bpmnum);
        }
        bpml.setCompanyId(this.getSessionUserCompanyId());
        bpmcService.addBpml(id,bpml);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 删除流程
     * @param id 流程ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/detleBpml")
    public ResultDTO detleBpml(@RequestParam("id") String id){
        bpmcService.detleBpml(id);
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 查看流程
     * @param id 流程ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/selectById")
    public ResultDTO selectById(@RequestParam("id") String id){
        return ResultDTO.successOf(workFlowStepService.getWorkFlowStepList
                (this.getSessionUserCompanyId(),id),
                GeneralResponseEnums.SUCCESS);
    }

    /**
     * 流程状态改变
     * @param id 流程ID
     * @param status 状态值
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/updateByIdAndStatus")
    public ResultDTO updateByIdAndStatus (@RequestParam("id") String id,
             @RequestParam("status") String status){
        return ResultDTO.successOf
                (bpmcService.updateByIdAndStatus(id,status),
                GeneralResponseEnums.SUCCESS);
    }


}
